<?php

namespace app\admin\dao;

use app\common\model\ConsultingMessage;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * ConsultingMessageDao 数据访问对象
 * Class ConsultingMessageDao
 * @package app\admin\dao
 * @method ConsultingMessage findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method ConsultingMessage insertActive(array $params, array $allow_field = [])
 * @method ConsultingMessage updateActive(ConsultingMessage $model, array $params, array $allow_field = [])
 * @method ConsultingMessage findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method ConsultingMessage updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(ConsultingMessage $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class ConsultingMessageDao
{
    use DaoTrait;

    /**
     * @var ConsultingMessage
     */
    private $baseModel;

    /**
     * ConsultingMessageDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = ConsultingMessage::class;
    }

    public function select($memberId, $consultingId, $page, $page_size)
    {
        return ConsultingMessage::where('consultingId', $consultingId)
            ->when(is_id($memberId), fn($query) => $query->where(function ($query) use ($memberId) {
                $query->whereOr('receiveId', $memberId)->whereOr('receiveId', 0);
            }))
            ->whereIn('messageType', [ConsultingMessage::TEXT, ConsultingMessage::IMAGE, ConsultingMessage::SYSTEM])
            ->with(['sender' => fn($query) => $query->sender()])
            ->order('id', 'DESC')
            ->paging($page, $page_size)
            ->select();
    }
}
